package com.erp.gather.controller;

import com.erp.gather.domain.bo.ErpProductBrandBo;
import com.erp.gather.domain.vo.ErpProductBrandVo;
import com.erp.gather.service.IErpProductBrandService;
import com.ruoyi.common.core.annotation.RepeatSubmit;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.web.oldController.BaseController;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.gather.api.domain.ErpProductBrand;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.*;

/**
 * 商品品牌
 *
 * @author erp
 * @date 2023-03-23
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/gather/productBrand")
public class ErpProductBrandController extends BaseController {

    private final IErpProductBrandService iErpProductBrandService;

    /**
     * 查询商品品牌列表
     */
    @GetMapping("/selectList")
    public R<Object> selectList(ErpProductBrandBo bo) {
    	List<ErpProductBrandVo> queryList = iErpProductBrandService.queryList(bo);
    	List<Map<String, Object>> list = new ArrayList<>();
    	queryList.forEach(vo -> {
    		Map<String, Object> map = new HashMap<>();
    		map.put("lable", vo.getBrandName());
    		map.put("value", vo.getId());
    		list.add(map);
    	});
    	return R.ok(list);
    }

    /**
     * 查询商品品牌列表
     */
    @GetMapping("/list")
    public TableDataInfo<ErpProductBrandVo> list(ErpProductBrandBo bo, PageQuery pageQuery) {
        return iErpProductBrandService.queryPageList(bo, pageQuery);
    }


    /**
     * 获取商品品牌详细信息
     *
     * @param id 主键
     */
    @GetMapping("/{id}")
    public R<ErpProductBrandVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iErpProductBrandService.queryById(id));
    }

    /**
     * 新增商品品牌
     */
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ErpProductBrandBo bo) {
        return toAjax(iErpProductBrandService.insertByBo(bo));
    }

    /**
     * 修改商品品牌
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ErpProductBrandBo bo) {
        return toAjax(iErpProductBrandService.updateByBo(bo));
    }

    /**
     * 删除商品品牌
     *
     * @param ids 主键串
     */
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iErpProductBrandService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    @GetMapping("/selectProductBrandAllList")
    public R<Object> selectProductBrandAllList(){
        return iErpProductBrandService.selectProductBrandAllList();
    }

    @PostMapping("/selectProductBrandInfo")
    public R<Object> selectProductBrandInfo(@RequestBody ErpProductBrand productBrandSel){
        return iErpProductBrandService.selectProductBrandInfo(productBrandSel);

    }

    @PostMapping("/selectProductBrandListInfo")
    public R<Object> selectProductBrandListInfo(@RequestBody ErpProductBrand productBrandSel){
        return iErpProductBrandService.selectProductBrandListInfo(productBrandSel);

    }

    @GetMapping("/getProductBrandById/{brandId}")
    public R<Object> getProductBrandById(@PathVariable("brandId") Long brandId){
        return iErpProductBrandService.getProductBrandById(brandId);

    }

    /**
     * @Description: 根据品牌名称获取品牌ID，若没有，直接新增，默认需要授权
     * @Author: j.zh
     * @Date: 2024/6/12
     * @param name
     * @return
     * @Version: 4.0.0
    **/
    @GetMapping("/getBrandByName/{name}")
    public String getBrandByName(@PathVariable("name") String name){
        return iErpProductBrandService.getProductBrandById(name);

    }

}
